package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.ItemDescMapper;
import com.taotao.mapper.ItemMapper;
import com.taotao.mapper.ItemParamMapper;
import com.taotao.pojo.Item;
import com.taotao.pojo.ItemDesc;
import com.taotao.pojo.ItemParam;
import com.taotao.service.ItemService;
import com.taotao.util.ExceptionUtil;
import com.taotao.util.IDUtils;
import com.taotao.util.TaotaoResult;
import com.taotao.util.model.EasyUIResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author hong.zheng
 * @Description
 * @date 2018-08-22-10:32
 **/
@Service
public class ItemServiceImpl implements ItemService {
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;
    @Autowired
    private ItemParamMapper itemParamMapper;

    @Override
    public TaotaoResult addItem(Item item,ItemDesc itemDesc,String itemParams) {
        try {
            //生成商品id
            //可以使用redis的自增长key，在没有redis之前使用时间+随机数策略生成
            Long itemId = IDUtils.genItemId();
            //补全不完整的字段
            item.setId(itemId);
            item.setStatus((byte) 1);
            Date date = new Date();
            item.setCreated(date);
            item.setUpdated(date);
            //把数据插入到商品表
            itemMapper.addItem(item);
            //添加商品描述
            itemDesc.setItemId(itemId);
            itemDesc.setCreated(date);
            itemDesc.setUpdated(date);

            //把数据插入到商品描述表
            itemDescMapper.addItemDesc(itemDesc);


            ItemParam itemParam = new ItemParam();
            itemParam.setItemId(itemId);
            itemParam.setParamData(itemParams);
            itemParam.setCreated(date);
            itemParam.setUpdated(date);

            itemParamMapper.insert(itemParam);

        } catch (Exception e) {
            LOGGER.error(e.getMessage(),e);
            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }

        return TaotaoResult.ok();
    }

    @Override
    public EasyUIResult getItemList(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<Item> list = itemMapper.selectItemList();
        PageInfo<Item> pageInfo = new PageInfo<Item>(list);
        long total = pageInfo.getTotal();
        EasyUIResult<Item> result = new EasyUIResult<Item>(total, list);
        return result;
    }

    @Override
    public Item getItemByItemId(Long itemId) {
        return itemMapper.getItemByItemId(itemId);
    }

    @Override
    public void updateItem(Item item,String desc,String itemParams) {

        Date updateDate = new Date();
        item.setUpdated(updateDate);
        itemMapper.updateItem(item);


        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemDesc(desc);
        itemDesc.setItemId(item.getId());
        itemDesc.setUpdated(new Date());
        itemDescMapper.updateItemDesc(itemDesc);

        ItemParam itemParam = new ItemParam();
        itemParam.setItemId(item.getId());
        itemParam.setParamData(itemParams);
        itemParam.setUpdated(updateDate);
        itemParamMapper.update(itemParam);

    }
}
